# _*_ coding:utf-8 _*_
#
from django import forms
from django.utils.translation import ugettext as _
from auser.models import User


class UserFormLogIn(forms.Form):
	username = forms.CharField(
		required=True,  # 要求必须填写
		label=u"用户名",
		error_messages={'required': '请输入用户名'},  # 错误信息提示
		widget=forms.TextInput(
			attrs={
				'placeholder': u"用户名",
			}
		),
	)
	password = forms.CharField(required=True,
	                           label=u"密码",
	                           error_messages={'required': u'请输入密码'},
	                           widget=forms.PasswordInput(
		                           attrs={
			                           'placeholder': u"密码",
		                           }
	                           ),
	                           )

	class Meta(object):
		model = User

	# 填表单时就开始执行

	def clean(self):
		if not self.is_valid():
			raise forms.ValidationError(u"用户名和密码为必填项")
		else:
			cleaned_data = super(UserFormLogIn, self).clean()
		return cleaned_data


class ChangepwdForm(forms.Form):
	oldpassword = forms.CharField(
		required=True,
		label=u"原密码",
		error_messages={'required': u'请输入原密码'},
		widget=forms.PasswordInput(
			attrs={
				'placeholder': u"原密码",
				"class": "form-control",
				"id": "exampleInputPassword1"

			}
		),
	)
	newpassword1 = forms.CharField(
		required=True,
		label=u"新密码",
		error_messages={'required': u'请输入新密码'},
		widget=forms.PasswordInput(
			attrs={
				'placeholder': u"新密码",
				"class": "form-control",
				"id": "exampleInputPassword1"
			}
		),
	)

	newpassword2 = forms.CharField(
		required=True,
		label=u"确认密码",
		error_messages={'required': u'请再次输入新密码'},
		widget=forms.PasswordInput(
			attrs={
				'placeholder': u"确认密码",
				"class": "form-control",
				"id": "exampleInputPassword1"
			}
		),
	)

	def clean(self):
		if not self.is_valid():
			raise forms.ValidationError(u"所有项都为必填项")
		elif self.cleaned_data['newpassword1'] != self.cleaned_data['newpassword2']:
			raise forms.ValidationError(u"两次输入的新密码不一样")
		else:
			cleaned_data = super(ChangepwdForm, self).clean()
		return cleaned_data


class ChangeEmailForm(forms.Form):
	email = forms.EmailField(
		required=True,
		label=u"邮箱",
		error_messages={'required': u"请输入正确的信息"},
		widget=forms.EmailInput(
			attrs={
				'placeholder': u"原来的邮箱",
				"class": "form-control",
				'aria-describedby': "basic-addon1"
			}
		)
	)
	email_new = forms.EmailField(
		required=True,
		label=u"邮箱",
		error_messages={'required': u"请输入正确的邮箱"},
		widget=forms.EmailInput(
			attrs={
				'placeholder': u"新邮箱",
				"class": "form-control",
				'aria-describedby': "basic-addon1"
			}
		)
	)

	def clean(self):
		if not self.is_valid():
			raise forms.ValidationError("请填入邮箱信息")
		else:
			clean_data = super(ChangeEmailForm, self).clean()
			return clean_data

	def clean_email_new(self):
		email = self.cleaned_data['email_new']
		if User.objects.filter(email=email).first():
			raise forms.ValidationError(_('重复的邮箱: %(mail)s'), code='invalid',
			                            params={'mail': email})
		return email


"""
字段验证(to_python,validate)，clean_field验证，clean()验证
"""
